<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class MaterialContract extends Model
{
    use HasFactory;


    protected $fillable = [
        'supplier_id',
        'title','project_id','type_id','user_id','material_id','price','tax_rate','tax_price','contract_type',

        'name', 'tax_id', 'address', 'mobile', 'bank', 'bank_number', 'bank_account', 'email', 'postal_code',

        'b_name', 'b_tax_id', 'b_address', 'b_mobile', 'b_bank', 'b_bank_number', 'b_bank_account', 'b_email', 'b_postal_code',

        'contract_canned','b_authorize_file', 'b_business_file', 'b_idcard_file', 'b_agent_idcard_file', 'b_legal', 'b_legal_mobile', 'b_agent', 'b_agent_mobile', 'deliver_address', 'receiver', 'sums', 'unit', 'unit_price', 'variety', 'pay_type', 'apply_id', 'liquidated_damages', 'approver', 'remarks'
    ];

    const POSTONLY = [
        'supplier_id',
        'title','project_id','type_id','user_id','material_id','price','tax_rate','tax_price','contract_type',

        'name', 'tax_id', 'address', 'mobile', 'bank', 'bank_number', 'bank_account', 'email', 'postal_code',

        'b_name', 'b_tax_id', 'b_address', 'b_mobile', 'b_bank', 'b_bank_number', 'b_bank_account', 'b_email', 'b_postal_code',

        'contract_canned','b_authorize_file', 'b_business_file', 'b_idcard_file', 'b_agent_idcard_file', 'b_legal', 'b_legal_mobile', 'b_agent', 'b_agent_mobile', 'deliver_address', 'receiver', 'sums', 'unit', 'unit_price', 'variety', 'pay_type', 'apply_id', 'liquidated_damages', 'approver', 'remarks'
    ];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }

    public function material()
    {
        return $this->hasOne(Material::class,'id','material_id')->select('id','title');
    }

    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->where('model','MaterialContract')->orderBy('id','desc');
    }

    public function reimburses()
    {
        return $this->hasMany(Reimburse::class,'source_id','id')->with('applicationApproved');
    }

    public function details()
    {
        return $this->hasMany(MaterialContractDetail::class,'material_contract_id','id');
    }


}
